September 2020
[W]e wanted users to be able to begin in an interactive environment, where they did not consciously think of themselves as programming. Then as their needs became clearer and their sophistication increased, they should be able to slide gradually into programming, when the language and system aspects would become more important.”
Low level:
High level(s):
R is great for
You might want to choose an other programming language for
If you type code directly in the console, will the objects you create occur in the environment frame? Could you imagine any problems occuring because of this?
When I want to close R, there is this pop-up window asking 'Save workspace image to ~/Documents/informatica/R/.RData?'. What does this mean? Is this useful?
Yes. All code you run will result in objects saved in the environment. You can click on the small broom symbol to delete all objects. A problem occurs when you change objects, but you do not consider that in your script.
When you save the workspace, all object you created within the environment will be retained. The workspace is saved to a hidden file called .RData, saved to your current working directory / project directory. Advantages of saving the workspace: You start at the same point, with the same objects you had, when you last closed your session. You do not need to recompute your objects, which can be time-intensive. Disadvantages: Your working directory is not in a clean state, when you start. You write code, so that you are able to reproduce your calculation, so no need to store objects. There are other ways to store objects, which take a long time to compute (-> rds-files). ")
firstname <- "Diren" surname <- "Senger" name <- paste(firstname, surname) # Concatenation name
## [1] "Diren Senger"
class(name) # Check the type
## [1] "character"
age <- as.character(25) # Casting class(age)
## [1] "character"
a <- 1 b <- 2 c <- a + b c
## [1] 3
d <- b*c/a d
## [1] 6
e <- d^2 e
## [1] 36
f <- as.integer(3.14) f
## [1] 3
a <- TRUE b <- FALSE # or a|b
## [1] TRUE
# and a&b
## [1] FALSE
# not !a
## [1] FALSE
4 == 5
## [1] FALSE
4 < 5
## [1] TRUE
4 >= 4
## [1] TRUE
somevariable <- NULL is.null(somevariable)
## [1] TRUE
# a simple numeric vector myvector <- c(1,4,6) myvector
## [1] 1 4 6
# a vector of type character
myfamily <- c("Paula", "Ali", "Emma")
myfamily
## [1] "Paula" "Ali" "Emma"
# get the first element of a vector myfamily[1]
## [1] "Paula"
# apply function to all items: myvector + 1
## [1] 2 5 7
paste(myfamily, "Meier")
## [1] "Paula Meier" "Ali Meier" "Emma Meier"
# concatenate vectors: longervector <- c(myvector, 5:7) longervector
## [1] 1 4 6 5 6 7
# create a sequence odd <- seq(from = 1, to = 10, by = 2) odd
## [1] 1 3 5 7 9
# create a boring sequence boring <- rep(1, 10) boring
## [1] 1 1 1 1 1 1 1 1 1 1
fac <- factor(c("good", "better", "best"))
levels(fac)
## [1] "best" "better" "good"
nlevels(fac)
## [1] 3
result <- list(data="fancy test data", mean=5, sd=1.32) result
## $data ## [1] "fancy test data" ## ## $mean ## [1] 5 ## ## $sd ## [1] 1.32
and annoying double brackets
myList <- list(smallLetters=letters[1:10],
capitalLetters=LETTERS[1:10],
numbers=1:5)
myList
## $smallLetters ## [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" ## ## $capitalLetters ## [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" ## ## $numbers ## [1] 1 2 3 4 5
and annoying double brackets
# Accessing multiple elements myList[1:2]
## $smallLetters ## [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" ## ## $capitalLetters ## [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J"
and annoying double brackets
# Accessing single elements myList[2][2]
## $<NA> ## NULL
myList[[2]][2]
## [1] "B"
myList[[1:2]]
## [1] "b"
m <- matrix(data=1:12, nrow = 3, ncol = 4) m
## [,1] [,2] [,3] [,4] ## [1,] 1 4 7 10 ## [2,] 2 5 8 11 ## [3,] 3 6 9 12
m
## [,1] [,2] [,3] [,4] ## [1,] 1 4 7 10 ## [2,] 2 5 8 11 ## [3,] 3 6 9 12
# Element-wise operations m * 0.5
## [,1] [,2] [,3] [,4] ## [1,] 0.5 2.0 3.5 5.0 ## [2,] 1.0 2.5 4.0 5.5 ## [3,] 1.5 3.0 4.5 6.0
m
## [,1] [,2] [,3] [,4] ## [1,] 1 4 7 10 ## [2,] 2 5 8 11 ## [3,] 3 6 9 12
# Element-wise operations m * c(1,2,3)
## [,1] [,2] [,3] [,4] ## [1,] 1 4 7 10 ## [2,] 4 10 16 22 ## [3,] 9 18 27 36
m
## [,1] [,2] [,3] [,4] ## [1,] 1 4 7 10 ## [2,] 2 5 8 11 ## [3,] 3 6 9 12
# Matrix multiplication m %*% c(1,2,3,4)
## [,1] ## [1,] 70 ## [2,] 80 ## [3,] 90
Good explanation of Matrix multiplication: Eli Bendersky's website
# Creating a data frame
myfamily <- c("Paula", "Ali", "Emma")
family.frame <- data.frame(index = 1:3,
firstname = myfamily,
surname = rep("Meier", 3))
| index | firstname | surname |
|---|---|---|
| 1 | Paula | Meier |
| 2 | Ali | Meier |
| 3 | Emma | Meier |
df <- read.table("beedatasmall.csv")
## Error in scan(file = file, what = what, sep = sep, quote = quote, dec = dec, : line 2 did not have 2 elements
df <- read.table("beedatasmall.csv", sep = ",")
| V1 | V2 | V3 | V4 | V5 | V6 | V7 | V8 | V9 | V10 |
|---|---|---|---|---|---|---|---|---|---|
| time | hive | h | t_i_1 | t_i_2 | t_i_3 | t_i_4 | t_i_5 | t_o | weight_kg |
| 1.565081418e+18 | 4 | NA | 27.25 | 26.375 | 23.4375 | 23.5 | 23.25 | 21.625 | NA |
| 1.565081428e+18 | 4 | NA | 27.25 | 26.4375 | 23.5 | 23.5 | 23.25 | 21.625 | NA |
| 1.565081431e+18 | 13 | 59.0554 | 22.875 | 22.1875 | 23 | 21.8125 | 22.6875 | 22.875 | 10.779 |
| 1.565081438e+18 | 4 | NA | 27.25 | 26.375 | 23.5 | 23.5625 | 23.25 | 21.625 | NA |
df <- read.table("beedatasmall.csv", sep = ",", header = T)
| time | hive | h | t_i_1 | t_i_2 | t_i_3 | t_i_4 | t_i_5 | t_o | weight_kg |
|---|---|---|---|---|---|---|---|---|---|
| 1.565081e+18 | 4 | NA | 27.250 | 26.3750 | 23.4375 | 23.5000 | 23.2500 | 21.625 | NA |
| 1.565081e+18 | 4 | NA | 27.250 | 26.4375 | 23.5000 | 23.5000 | 23.2500 | 21.625 | NA |
| 1.565081e+18 | 13 | 59.0554 | 22.875 | 22.1875 | 23.0000 | 21.8125 | 22.6875 | 22.875 | 10.779 |
| 1.565081e+18 | 4 | NA | 27.250 | 26.3750 | 23.5000 | 23.5625 | 23.2500 | 21.625 | NA |
| 1.565081e+18 | 4 | NA | 27.250 | 26.3750 | 23.5000 | 23.5625 | 23.2500 | 21.625 | NA |
df <- read.csv("beedatasmall.csv")
| time | hive | h | t_i_1 | t_i_2 | t_i_3 | t_i_4 | t_i_5 | t_o | weight_kg |
|---|---|---|---|---|---|---|---|---|---|
| 1.565081e+18 | 4 | NA | 27.250 | 26.3750 | 23.4375 | 23.5000 | 23.2500 | 21.625 | NA |
| 1.565081e+18 | 4 | NA | 27.250 | 26.4375 | 23.5000 | 23.5000 | 23.2500 | 21.625 | NA |
| 1.565081e+18 | 13 | 59.0554 | 22.875 | 22.1875 | 23.0000 | 21.8125 | 22.6875 | 22.875 | 10.779 |
| 1.565081e+18 | 4 | NA | 27.250 | 26.3750 | 23.5000 | 23.5625 | 23.2500 | 21.625 | NA |
| 1.565081e+18 | 4 | NA | 27.250 | 26.3750 | 23.5000 | 23.5625 | 23.2500 | 21.625 | NA |
df <- read.csv("sampleData.csv")
| name..age..subject..year | |
|---|---|
| Li; 18; chemistry; 1 | |
| Svenson | Jacob; 25; psychology; 12 |
| Raphaela; 23; psychology; 1 |
df <- read.csv("sampleData.csv", sep=";")
| name | age | subject | year |
|---|---|---|---|
| Li | 18 | chemistry | 1 |
| Svenson,Jacob | 25 | psychology | 12 |
| Raphaela | 23 | psychology | 1 |
df <- read.csv("beedata.csv")
str(df)
## 'data.frame': 171299 obs. of 10 variables: ## $ time : num 1.57e+18 1.57e+18 1.57e+18 1.57e+18 1.57e+18 ... ## $ hive : int 4 4 13 4 4 4 4 4 4 4 ... ## $ h : num NA NA 59.1 NA NA ... ## $ t_i_1 : num 27.2 27.2 22.9 27.2 27.2 ... ## $ t_i_2 : num 26.4 26.4 22.2 26.4 26.4 ... ## $ t_i_3 : num 23.4 23.5 23 23.5 23.5 ... ## $ t_i_4 : num 23.5 23.5 21.8 23.6 23.6 ... ## $ t_i_5 : num 23.2 23.2 22.7 23.2 23.2 ... ## $ t_o : num 21.6 21.6 22.9 21.6 21.6 ... ## $ weight_kg: num NA NA 10.8 NA NA ...
E.g. converting kg to g:
df$weight_g <- df$weight_kg*1000
| time | hive | h | t_i_1 | weight_kg | weight_g |
|---|---|---|---|---|---|
| 1.565081e+18 | 4 | NA | 27.250 | NA | NA |
| 1.565081e+18 | 4 | NA | 27.250 | NA | NA |
| 1.565081e+18 | 13 | 59.0554 | 22.875 | 10.779 | 10779 |
| 1.565081e+18 | 4 | NA | 27.250 | NA | NA |
| 1.565081e+18 | 4 | NA | 27.250 | NA | NA |
Marking high temperature values:
df$highTemp <- df$t_i_1>25
| time | hive | h | t_i_1 | weight_kg | weight_g | highTemp |
|---|---|---|---|---|---|---|
| 1.565081e+18 | 4 | NA | 27.250 | NA | NA | TRUE |
| 1.565081e+18 | 4 | NA | 27.250 | NA | NA | TRUE |
| 1.565081e+18 | 13 | 59.0554 | 22.875 | 10.779 | 10779 | FALSE |
| 1.565081e+18 | 4 | NA | 27.250 | NA | NA | TRUE |
| 1.565081e+18 | 4 | NA | 27.250 | NA | NA | TRUE |
Nanoseconds to seconds:
df$timeSeconds <- df$time/1000000000
| time | hive | h | t_i_1 | weight_kg | weight_g | highTemp | timeSeconds |
|---|---|---|---|---|---|---|---|
| 1.565081e+18 | 4 | NA | 27.250 | NA | NA | TRUE | 1565081418 |
| 1.565081e+18 | 4 | NA | 27.250 | NA | NA | TRUE | 1565081428 |
| 1.565081e+18 | 13 | 59.0554 | 22.875 | 10.779 | 10779 | FALSE | 1565081431 |
| 1.565081e+18 | 4 | NA | 27.250 | NA | NA | TRUE | 1565081438 |
| 1.565081e+18 | 4 | NA | 27.250 | NA | NA | TRUE | 1565081448 |
Dealing with the timestamp
df$time <- as.POSIXct(df$timeSeconds, origin="1970-01-01")
| time | hive | h | t_i_1 | weight_kg | weight_g | highTemp | timeSeconds |
|---|---|---|---|---|---|---|---|
| 2019-08-06 10:50:18 | 4 | NA | 27.250 | NA | NA | TRUE | 1565081418 |
| 2019-08-06 10:50:28 | 4 | NA | 27.250 | NA | NA | TRUE | 1565081428 |
| 2019-08-06 10:50:31 | 13 | 59.0554 | 22.875 | 10.779 | 10779 | FALSE | 1565081431 |
| 2019-08-06 10:50:38 | 4 | NA | 27.250 | NA | NA | TRUE | 1565081438 |
| 2019-08-06 10:50:48 | 4 | NA | 27.250 | NA | NA | TRUE | 1565081448 |
df[choose_rows, choose_columns]
with some boolean expressions
Select and plot data from hive 4:
df.4 <- df[df$hive==4,] plot(df.4$time, df.4$t_o, ylim=c(0,40))
Select only the first 1000 lines:
df.4.sub <- df.4[1:1000,] plot(df.4.sub$time, df.4.sub$t_o, ylim=c(0,40))
Delete columns: / Choose only some columns
names(df)
## [1] "time" "hive" "h" "t_i_1" "t_i_2" ## [6] "t_i_3" "t_i_4" "t_i_5" "t_o" "weight_kg" ## [11] "weight_g" "highTemp" "timeSeconds"
df.some <- df[, c(1,2,10)]
| time | hive | weight_kg |
|---|---|---|
| 2019-08-06 10:50:18 | 4 | NA |
| 2019-08-06 10:50:28 | 4 | NA |
| 2019-08-06 10:50:31 | 13 | 10.779 |
| 2019-08-06 10:50:38 | 4 | NA |
| 2019-08-06 10:50:48 | 4 | NA |
You can also use:
df.same <- df[, c("time", "hive", "weight_kg")]
| time | hive | weight_kg |
|---|---|---|
| 2019-08-06 10:50:18 | 4 | NA |
| 2019-08-06 10:50:28 | 4 | NA |
| 2019-08-06 10:50:31 | 13 | 10.779 |
| 2019-08-06 10:50:38 | 4 | NA |
| 2019-08-06 10:50:48 | 4 | NA |
You can also use:
df.or <- df[, -c(3:9,11:13)]
| time | hive | weight_kg |
|---|---|---|
| 2019-08-06 10:50:18 | 4 | NA |
| 2019-08-06 10:50:28 | 4 | NA |
| 2019-08-06 10:50:31 | 13 | 10.779 |
| 2019-08-06 10:50:38 | 4 | NA |
| 2019-08-06 10:50:48 | 4 | NA |
a <- 19
if(a >= 18){
print("Yes, you are allowed to see this content.")
}
## [1] "Yes, you are allowed to see this content."
goodWeather <- TRUE
if (goodWeather){
print("Let's go to the beach!")
} else{
print("Let's eat pizza.")
}
## [1] "Let's go to the beach!"
do.you.want.this <- "chocolate"
#do.you.want.this <- "cookies"
#do.you.want.this <- "carrots"
if (do.you.want.this == "chocolate"){
print("Yes, I love chocolate!")
} else if (do.you.want.this == "cookies"){
print("Yes, if they are with chocolate.")
} else {
print("Hm, not sure. Do you have chocolate?")
}
## [1] "Yes, I love chocolate!"
library(lubridate)
birthday <- as.POSIXct("2019-08-06 10:50:18")
what.do.you.want.to.know <- "year"
#what.do.you.want.to.know <- "month"
#what.do.you.want.to.know <- "chocolate"
if (what.do.you.want.to.know == "year"){
print(year(birthday))
} else if (what.do.you.want.to.know == "month"){
print(month(birthday))
} else {
print("Sorry, what do you want to know?")
}
## [1] 2019
raincoat <- T
rain <- T
if(raincoat){
if(rain){
print("dry")
} else{
print("useless raincoat")
}
} else{
if(rain){
print("wet")
} else{
print("dry")
}
}
## [1] "dry"
myfamily <- list(Paula=31, Ali=29, Emma=5)
age <- 0
for (i in 1:length(myfamily)){
age <- age + myfamily[[i]]
}
age
## [1] 65
ages <- c(31, 29, 5)
total.age <- 0
for(i in ages){
total.age <- total.age + i
}
total.age
## [1] 65
for (i in 1:ncol(df)){
name <- names(df)[i]
colclass <- class(df[,i])
print(paste(name, ":", colclass))
}
## [1] "time : POSIXct" "time : POSIXt" ## [1] "hive : integer" ## [1] "h : numeric" ## [1] "t_i_1 : numeric" ## [1] "t_i_2 : numeric" ## [1] "t_i_3 : numeric" ## [1] "t_i_4 : numeric" ## [1] "t_i_5 : numeric" ## [1] "t_o : numeric" ## [1] "weight_kg : numeric" ## [1] "weight_g : numeric" ## [1] "highTemp : logical" ## [1] "timeSeconds : numeric"
df <- read.csv("beedata.csv")
print("mean of h:")
mean(df$h)
print("mean of t_i_1:")
mean(df$t_i_1)
print("mean of weight_kg:")
mean(df$weight_kg)
print("missing h values:")
sum(is.na(df$h))
print("missing t_i_1 values:")
sum(is.na(df$h))
print("missing weight_kg values:")
sum(is.na(df$h))
to.analyse <- c("h", "t_i_1", "weight_kg")
for(i in 1:length(to.analyse)){
print(paste("mean of", to.analyse[i], ":"))
print(mean(to.analyse[i]))
print(paste("missing", to.analyse[i], "values:"))
print(sum(is.na(df[,to.analyse[i]])))
}
apply(df, 2, class)
## time hive h t_i_1 t_i_2 t_i_3 ## "character" "character" "character" "character" "character" "character" ## t_i_4 t_i_5 t_o weight_kg weight_g highTemp ## "character" "character" "character" "character" "character" "character" ## timeSeconds ## "character"
lapply(df, class)
## $time ## [1] "POSIXct" "POSIXt" ## ## $hive ## [1] "integer" ## ## $h ## [1] "numeric" ## ## $t_i_1 ## [1] "numeric" ## ## $t_i_2 ## [1] "numeric" ## ## $t_i_3 ## [1] "numeric" ## ## $t_i_4 ## [1] "numeric" ## ## $t_i_5 ## [1] "numeric" ## ## $t_o ## [1] "numeric" ## ## $weight_kg ## [1] "numeric" ## ## $weight_g ## [1] "numeric" ## ## $highTemp ## [1] "logical" ## ## $timeSeconds ## [1] "numeric"
sapply(df, class)
## $time ## [1] "POSIXct" "POSIXt" ## ## $hive ## [1] "integer" ## ## $h ## [1] "numeric" ## ## $t_i_1 ## [1] "numeric" ## ## $t_i_2 ## [1] "numeric" ## ## $t_i_3 ## [1] "numeric" ## ## $t_i_4 ## [1] "numeric" ## ## $t_i_5 ## [1] "numeric" ## ## $t_o ## [1] "numeric" ## ## $weight_kg ## [1] "numeric" ## ## $weight_g ## [1] "numeric" ## ## $highTemp ## [1] "logical" ## ## $timeSeconds ## [1] "numeric"
a <- 2
while (a<1000){
a <- a^2
print(a)
}
## [1] 4 ## [1] 16 ## [1] 256 ## [1] 65536
letters <- letters[]
repeat{
i <- as.integer(runif(1,1,26))
letter <- letters[i]
print(letter)
if(letter=='o'){
break
}
}
## [1] "o"
a <- 0.5
repeat{
a <- a - sqrt(a)
print(a)
if(sqrt(a) > a){
break
}
}
## [1] -0.2071068
## Warning in sqrt(a): NaNs produced
## Error in if (sqrt(a) > a) {: missing value where TRUE/FALSE needed
https://me.me/i/while-not-edge-do-run-run-while-not-edge-5b875ee64adb4e939ee50ad394309509
When do you use for? When do you use while? Can you think of examples?
myPlus <- function(a, b){
return(a + b)
}
myPlus(1,2)
## [1] 3
myPlus <- function(a=1, b=1){
return(a + b)
}
myPlus(1,2)
## [1] 3
myPlus()
## [1] 2
dataframe.info <- function(df){
cells.count <- ncol(df)*nrow(df)
return(list(columns=ncol(df), rows=nrow(df), cells= cells.count))
}
dataframe.info(df)
## $columns ## [1] 13 ## ## $rows ## [1] 171299 ## ## $cells ## [1] 2226887
funny.words <- function(s = "summer", count = 3){
s = "summer"
s1 <- ""
for(i in 1:2){
for(i in 1:count){
print(substr(s, i, i))
s1 <- paste(s1, substr(s, i, i), sep = "")
}
}
return(s1)
}
funny.words()
## [1] "s" ## [1] "u" ## [1] "m" ## [1] "s" ## [1] "u" ## [1] "m"
## [1] "sumsum"
funny.words("banana", 5)
## [1] "s" ## [1] "u" ## [1] "m" ## [1] "m" ## [1] "e" ## [1] "s" ## [1] "u" ## [1] "m" ## [1] "m" ## [1] "e"
## [1] "summesumme"
Use the debugging options to find all mistakes in the funny.words function.
df.4 <- df[df$hive==4,] # plot temperature outside plot(df.4$time, df.4$t_i_3)
# plot temperature outside
plot(df.4$time, df.4$t_i_3,
ylim=c(0,40)) # added this
# plot temperature outside
plot(df.4$time, df.4$t_i_3,
ylim = c(0,40),
xlab = "Time (2019)", # added this
ylab = "Temperature within hive", # added this
main = "Sensor measurements") # added this
# plot temperature outside
plot(df.4$time, df.4$t_i_3,
ylim = c(0,40),
type = "b", # added this
lty = 1, # added this
xlab = "Time (2019)",
ylab = "Temperature within hive",
main = "Sensor measurements")
Source: http://www.sthda.com/english/wiki/line-types-in-r-lty
df.4 <- df.4[df.4$t_i_3>5&df.4$t_i_3<40,] # added this
# plot temperature outside
plot(df.4$time, df.4$t_i_3,
ylim = c(0,40),
type = "b",
lty = 1,
xlab = "Time (2019)",
ylab = "Temperature within hive",
main = "Sensor measurements")
# plot temperature outside
plot(df.4$time, df.4$t_i_3,
ylim = c(0,40),
type = "b",
lty = 1,
pch = 4, # added this
xlab = "Time (2019)",
ylab = "Temperature within hive",
main = "Sensor measurements")
Source: http://www.sthda.com/english/wiki/r-plot-pch-symbols-the-different-point-shapes-available-in-r
# plot temperature outside
plot(df.4$time, df.4$t_i_3,
ylim = c(0,40),
type = "b",
lty = 1,
pch = 4,
xlim = as.POSIXct(c("2019-08-08", "2019-08-09")), # added this
xlab = "Time (2019-08-08)",
ylab = "Temperature within hive",
main = "Sensor measurements")
# plot temperature outside
plot(df.4$time, df.4$t_i_3,
ylim = c(0,40),
type = "b",
lty = 1,
pch = 4,
xlim = as.POSIXct(c("2019-08-08", "2019-08-09")),
xlab = "Time (2019-08-08)",
ylab = "Temperature within hive",
main = "Sensor measurements",
xaxt="n")
axis.POSIXct(1,
at=seq(min(df.4$time), max(df.4$time), by="1 hour"),
format="%H:00") # added this
# subset data
df.4 <- df[df$hive==4,]
# plot temperature outside
plot(df.4$time, df.4$t_o, ylim=c(0,40),type = 'p', pch=4)
# choose colours
cl <- rainbow(5)
# choose colums
cols <- 4:8
# plot each column
for (i in 1:5){
lines(df.4$time,
df.4[,cols[i]],
col = cl[i],
type = 'p',
pch=4,
ylim=c(0,40))
}
# add legend
legend("topright", legend=c(1, 2, 3, 4, 5, "outside"),
col=c(cl, "black"), pch = 4, lty = 0, cex=0.8)
# add legend
legend("topright", legend=c(1, 2, 3, 4, 5, "outside"),
col=c(cl, "black"), pch = 4, lty = 0, cex=0.8)
# plot data library(ggplot2) ggplot(data = df.4, aes(x=time, y=t_i_3)) + geom_point()
# plot data
library(ggplot2)
ggplot(data = df.4, aes(x=time, y=t_i_3)) + geom_point(shape=4) +
ylim(c(0, 40)) +
xlab("Time (2019") +
ylab("Temperature within hive") +
ggtitle("Sensor measurements")
## Warning: Removed 152 rows containing missing values (geom_point).
# subset data
df.4 <- df[df$hive==4,]
# choose columns
df.4.cols <- df.4[,c(1,4:9)]
# reshape data
library(reshape)
mdf <- melt(df.4.cols, id=c("time"))
# plot data
library(ggplot2)
ggplot(data = mdf, aes(x=time, y=value)) +
geom_line(aes(colour=variable)) +
ylim(c(0, 40))
library(plotly) fig <- plot_ly(df.4[1:100,], x = ~time, y = ~t_i_3) fig
png("test.png")
plot(hist(rnorm(100)))
dev.off()
library(magrittr) x <- 9 # Calculate the square-root of x sqrt(x)
## [1] 3
# Calculate it using pipes x %>% sqrt
## [1] 3
x <- 9 # Calculate the square-root of x and update x x <- sqrt(x) x
## [1] 3
# Calculate it using pipes x <- 9 x %<>% sqrt x
## [1] 3
nrow(subset(df, hive==4))
## [1] 60193
df %>% subset(hive==4) %>% nrow
## [1] 60193
Let's look at the homework together
You can use it for basically everything.
# html --- title: "R Markdown Example" author: "Somebody" date: "August 14, 2019" output: html_document ---
# word --- title: "R Markdown Example" author: "Somebody" date: "August 14, 2019" output: word_document ---
---
title: "R Markdown Example"
author: "Somebody"
date: "August 14, 2019"
output:
html_document:
toc: true # print a table of content
toc_depth: 2 # maximal depth of the table of content
number_sections: true # automatically number the sections
code_folding: hide # have buttons to show/ hide code
theme: united # specify a theme
toc_float: true # generate a navigation bar
---
# Header 1 ## Header 2 ### Header 3
Use the insert button. You have following further options:
* unordered list
+ sub-item 1
+ sub-item 2
- sub-sub-item 1
* item 2
1. ordered list
2. item 2
i) sub-item 1

A good documentation of R-Markdown can be found here: https://bookdown.org/yihui/rmarkdown/
And here is a cheat sheet: https://www.rstudio.com/resources/cheatsheets/#rmarkdown